#include "messager.h"
#include "ams.h"
#include "log.h"
#include "lvgl.h"
#include "rtc_misc.h"

LV_IMG_DECLARE(icon_heart);
LV_IMG_DECLARE(icon_rt_hr_animation_0);
LV_IMG_DECLARE(icon_rt_hr_animation_1);
LV_IMG_DECLARE(icon_rt_hr_animation_2);
LV_IMG_DECLARE(icon_rt_hr_animation_3);
LV_IMG_DECLARE(icon_rt_hr_animation_4);
LV_IMG_DECLARE(icon_rt_hr_animation_5);
LV_IMG_DECLARE(icon_rt_hr_animation_6);
LV_IMG_DECLARE(icon_rt_hr_animation_7);

ACTIVITY_OPS_IMPLEMENT_DECLARE(heartrate, &icon_heart, ACTIVITY_ATTR_FLAG_APP);

static const lv_img_dsc_t *array[] =
{
    &icon_rt_hr_animation_0,
    &icon_rt_hr_animation_1,
    &icon_rt_hr_animation_2,
    &icon_rt_hr_animation_3,
    &icon_rt_hr_animation_4,
    &icon_rt_hr_animation_5,
    &icon_rt_hr_animation_6,
    &icon_rt_hr_animation_7,
};

// ##
struct hr_user_t
{
    void *hr_icon;
    void *hr_value;
    void *hr_icon_update_task;
    void *hr_value_update_task;
};

// ##
static void app_start(void *content)
{

}

// ##
static void app_stop(void *content)
{

}

// ##
static void hr_task_anim_handle(struct _lv_task_t *arg)
{
    lv_obj_t *hr_icon = (lv_obj_t *)arg->user_data;;
    static uint8_t cnt = 0;
    cnt++;
    cnt %= sizeof(array) / sizeof(array[0]);
    lv_img_set_src(hr_icon, array[cnt]);
}

// ##
static void hr_task_value_handle(struct _lv_task_t *arg)
{
    lv_obj_t *hr_value = (lv_obj_t *)arg->user_data;;
    lv_label_set_text_fmt(hr_value, "%d", (rand() % 60) + 120);
}

// ##
static void app_prepare(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    activity->custom = ams_malloc(sizeof(struct hr_user_t));
}

// ##
static void *app_create(void *content)
{
    static lv_style_t style;
    lv_style_init(&style);

    lv_style_set_pad_top(&style, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_bottom(&style, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_left(&style, LV_STATE_DEFAULT, 10);
    lv_style_set_pad_right(&style, LV_STATE_DEFAULT, 10);

    lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_RED);
    lv_style_set_text_letter_space(&style, LV_STATE_DEFAULT, 0);
    lv_style_set_text_line_space(&style, LV_STATE_DEFAULT, 20);
    lv_style_set_text_decor(&style, LV_STATE_DEFAULT, LV_TEXT_DECOR_NONE);

    lv_style_set_text_font(&style, LV_STATE_DEFAULT, &lv_font_montserrat_40);
    lv_style_set_text_color(&style, LV_STATE_DEFAULT, lv_color_hex(0xFF0000));

    // ##
    ams_activity_t *activity = (ams_activity_t *)content;
    activity->ui = lv_obj_create(NULL, NULL);

    struct hr_user_t *user = (struct hr_user_t *)activity->custom;
    user->hr_icon = lv_img_create(activity->ui, NULL);
    lv_img_set_src(user->hr_icon, &icon_rt_hr_animation_0);
    lv_obj_set_pos(user->hr_icon, (LV_HOR_RES - GET_IMAGE_W(&icon_rt_hr_animation_0)) / 2, 40);
    lv_task_create(hr_task_anim_handle, 100, LV_TASK_PRIO_MID, user->hr_icon);

    // ##
    user->hr_value = lv_label_create(activity->ui, NULL);
    lv_obj_add_style(user->hr_value, LV_LABEL_PART_MAIN, &style);
    lv_obj_align(user->hr_value, activity->ui, LV_ALIGN_CENTER, 0, 0);
    lv_label_set_text_fmt(user->hr_value, "%d", 120);
    lv_task_create(hr_task_value_handle, 800, LV_TASK_PRIO_MID, user->hr_value);

    return activity->ui;
}

// ##
static void *app_destroy(void *content)
{
    return NULL;
}

// ##
static void app_event_handle(void *event)
{

}

// ##
static void app_gesture_handle(void *event)
{
    ams_gesture_t evt = *((ams_gesture_t *)event);
    switch(evt)
    {
        case GESTURE_SLIDE_TO_TOP:
            break;
        case GESTURE_SLIDE_TO_BOTTOM:
            break;
        case GESTURE_SLIDE_TO_LEFT:
            break;
        case GESTURE_SLIDE_TO_RIGHT:
            ams_switch_by_name("applist", DIR_SLIDE_TO_RIGHT);
            break;
    }
}

